﻿
@{
    ViewBag.Title = "工单签到";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
<link rel="stylesheet" href="~/Content/css/easyui.css" />
<link rel="stylesheet" href="~/Content/css/weui.min.css">
<link rel="stylesheet" href="~/Content/css/jquery-weui.min.css">
<script src="~/Content/script/jquery-1.11.0.min.js"></script>
<script src="~/Content/script/jquery-weui-1.2.1.min.js"></script>
<script src="~/Content/script/jquery.easyui.min.js"></script>
<script src="~/Content/script/jquery.easyui-1.4.3.min.js"></script>

<div class="weui-cells weui-cells_form" style="bottom: 20px;">
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">油机编号:</label></div>
        <div class="weui-cell__bd">
            <input id="genset" class="weui-input" type="text" placeholder="扫描油机二维码">
        </div>
        <div id="gScan" class="weui-btn weui-btn_mini weui-btn_primary">扫描</div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">采集器ID:</label></div>
        <div class="weui-cell__bd">
            <input id="dtuid" class="weui-input" type="text" placeholder="扫描模块二维码">
        </div>
        <div id="cScan" class="weui-btn weui-btn_mini weui-btn_primary">扫描</div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">发电给:</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="GenerationFor" type="text" readonly="readonly" placeholder="选择发电给">
        </div>
    </div>



</div>
<div class="weui-cells__tips">*以上油机信息均为必填项</div>

@*<div class="ui-btn-wrap" id="footer">
        <button class="ui-btn-primary">
            保存
        </button>
    </div>*@

<div class="weui-btn-area">
    <div id="submit" class="weui-btn weui-btn_primary">签到</div>
</div>



<script src="~/Content/script/jweixin-1.4.0.js"></script>
<script>

    //获取Detail传过来的参数
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


    //选择给哪个运营商发电
    $("#GenerationFor").select({
        title: "选择运营商",
        items: [
            {
                title: "电信",
                value: "电信",
            },
            {
                title: "移动",
                value: "移动",
            },
            {
                title: "联通",
                value: "联通",
            },
            {
                title: "联通,电信",
                value: "联通,电信",
            },
            {
                title: "移动,联通",
                value: "移动,联通",
            },
            {
                title: "移动,电信",
                value: "移动,电信",
            },
            {
                title: "移动,联通,电信",
                value: "移动,联通,电信",
            }
        ]
    })

    var annouceCode = GetRequest()['annouceCode'];//拿到参数annouceCode
    var AnnounceStatus = GetRequest()['AnnounceStatus'];//拿到参数AnnounceStatus
    //微信jdk
    $(document).ready(function () {
        $.ajax({
            url: '/WeiXin/JsapiTicketSignature?fullUrl=' + encodeURIComponent(window.location.href) + '',
            type: 'get',
            //beforeSend: function () {
            //    $.showLoading("加载中...");
            //},
            success: function (data) {
                //$.hideLoading();
                var result = eval(data);
                // wx.config成功后才会触发wx.ready
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: result.appId, // 必填，公众号的唯一标识
                    timestamp: result.timestamp, // 必填，生成签名的时间戳
                    nonceStr: result.nonceStr, // 必填，生成签名的随机串
                    signature: result.signature,// 必填，签名
                    jsApiList: ["scanQRCode", "getLocation"] // 必填，需要使用的JS接口列表
                });
            }
        });
        wx.ready(function () {
            //油机扫描事件
            $("#gScan").click(function () {
                wx.scanQRCode({
                    needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                    scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                    success: function (res) {
                        var gScanCode = res.resultStr; // 当needResult 为 1 时，扫码返回的结果

                        $("#genset").val('');
                        $.ajax({
                            url: '/api/Annouce/GetGenSetOrDtuidInfo?type=G&paraStr=' + gScanCode,
                            type: 'get',
                            beforeSend: function () {
                                $.showLoading("加载中...");
                            },
                            success: function (data) {
                               // alert(JSON.stringify(data));
                                $.hideLoading();
                                //var json = JSON.parse(data);//json转数据
                                var json = data;//json转数据
                                if (json.Code == 0) {
                                    $.alert(json.Result, "提示");
                                } else if (json.Result.DTUID != "" || json.Result.DTUID != null) {
                                    $("#dtuid").val(json.Result.DTUID);
                                }
                                $("#genset").val(gScanCode);
                            },
                            error: function (data) {
                                alert(JSON.stringify(data));
                            }
                        });
                        //getGenSetOrDtuidInfo("G", result, function () {
                        //    $("#genset").val(result);
                        //});
                    }
                });
            });
            //采集器扫描事件
            $("#cScan").click(function () {
                wx.scanQRCode({
                    needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                    scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                    success: function (res) {
                        var cScanCode = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                        $("#dtuid").val('');
                        $.ajax({
                            url: '/api/Annouce/GetGenSetOrDtuidInfo?type=C&paraStr=' + cScanCode,
                            type: 'get',
                            beforeSend: function () {
                                $.showLoading("加载中...");
                            },
                            success: function (data) {
                                $.hideLoading();
                                //var json = JSON.parse(data);//json转数据
                                var json = data;//json转数据
                                if (json.Code == 0) {
                                    $.alert(json.Result, "提示");
                                } else if (json.Result.GensetNum != "" || json.Result.GensetNum != null) {
                                    $("#genset").val(json.Result.GensetNum);
                                }
                                $("#dtuid").val(cScanCode);
                            },
                            error: function (data) {
                                alert(JSON.stringify(data));
                            }
                        });
                        //getGenSetOrDtuidInfo("C", result, function () {
                        //    $("#dtuid").val(result);
                        //});
                    }
                });
            });

            //绑定事件
            $("#submit").click(function () {
                wx.getLocation({
                    type: 'wgs84',// 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                    success: function (res) {
                        var isErr = false;//全局变量，是否错误
                        //发电给
                        var generationFor = $("#GenerationFor").data('values');
                        //检查是否为空
                        $("input").each(function () {
                            if ($(this).val() === "") {
                                isErr = true;
                            }
                        });
                        if (generationFor == 'undefined') {
                            isErr = true;
                        }
                        if (isErr) {
                            isErr = false;
                            $.alert("请输入完整信息!", "提示");
                            return;
                        }
                        $.confirm("是否执行此操作?", "提示", function () {
                            //用户处理通知单(接单,签到,扫码绑定油机)
                            $.ajax({
                                url: '/api/Annouce/UserProcessAnnouce?annouceCode=' + annouceCode + '&AnnounceStatus=' + AnnounceStatus + '&longitude=' + res.longitude + '&latitude=' + res.latitude + '&gensetNum=' + $("#genset").val() + '&generationFor=' + generationFor + '&registeredDtuId=' + $("#dtuid").val() + '',
                                type: 'get',
                                beforeSend: function () {
                                    $.showLoading("提交中...");
                                },
                                success: function (res) {
                                    $.hideLoading();
                                    console.log(res)
                                    res = res.Data
                                    if (res.Code === 1) {
                                        $.toast("操作成功!");
                                        setTimeout(function () {
                                            location.href = "/AnnouceList/Detail?annouceCode=" + annouceCode + "";
                                        }, 1 * 1000);
                                    } else {
                                        $.alert(res.Result, "提示");
                                    }
                                }
                            })
                        }, function () {

                        })
                    }
                })
            })

        });


    })

</script>